package com.tmxk.api.days01.yc.set;

import com.tmxk.api.days01.yc.Dog;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

/**
 *@Author: yc
 *@CreateTime: 2022-11-28  16:06
 *@Description: Set集合框架
 *@Version: 1.0
 */
public class SetDemo {
    public static void main(String[] args) {
        Dog dog1 = new Dog("小花",2);
        Dog dog2 = new Dog("花花",3);
        Dog dog3 = new Dog("小黑",1);
        Dog dog4 = new Dog("小白",5);
        Dog dog5 = new Dog("汪汪",6);

        //Set<Dog> dogSet = new HashSet<>();
        HashSet<Dog> dogSet = new HashSet<>();
        dogSet.add(dog1);
        dogSet.add(dog2);
        dogSet.add(dog3);
        dogSet.add(dog4);
        dogSet.add(dog5);
        System.out.println("dogSet的长度" + dogSet.size());
        System.out.println("使用for循环遍历  方式一");
        for(Dog dog : dogSet){
            System.out.print(dog.getName() + "\t" + dog.getAge() + "\t");
        }
        System.out.println("\n使用lambda表达式遍历  方式二");
        dogSet.forEach(dog -> {
            System.out.print(dog.getName() + "\t" + dog.getAge() + "\t");
        });
        System.out.println("\n使用迭代器集合元素遍历  方式三");
        Iterator<Dog> it = dogSet.iterator();
        while(it.hasNext()){
            Dog dog = it.next();
            System.out.print(dog.getName() + "\t" + dog.getAge() + "\t");
        }

        //TreeSet存储的数据可排序, 该集合框架底层实现 Comparable 接口, 进行数据比较
        TreeSet<Dog> dogTreeSet = new TreeSet();
        dogTreeSet.add(dog1);
        dogTreeSet.add(dog2);
        dogTreeSet.add(dog3);
        dogTreeSet.add(dog4);
        dogTreeSet.add(dog5);
        System.out.println("dogTreeSet的集合长度是:" + dogTreeSet.size());
        System.out.println("循环遍历dogTreeSet 集合 ");
        for (Object obj : dogTreeSet ){
            System.out.println(obj);
        }
    }
}
